package com.yilijk.base.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import androidx.annotation.NonNull;


import com.yilijk.base.view.LoadingDialog;

import org.jetbrains.annotations.NotNull;


public class LoadingUtils {


    private static LoadingDialog loadingDialog;

    private static LoadingUtils loadingUtils;

    private static Context mContext;

    private int sendTime = 10 * 1000;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull @NotNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                dismissLoading();
            }
        }
    };

    public static synchronized LoadingUtils getLoadingUtils(Context context) {
        mContext = context;
        if (loadingUtils == null) {
            loadingUtils = new LoadingUtils();
        }
        if (context == null) {
            //todo context is null
        } else if (context instanceof Activity && ((Activity) context).isFinishing()) {
            //todo activity is null or activity is finish
        } else {
            createLoading(context);
        }
        return loadingUtils;
    }


    public static void createLoading(Context context) {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(context);
        } else {
            if (context instanceof Activity) {
                Activity activity = (Activity) context;
                if (loadingDialog.isShowing() && activity != null && !activity.isFinishing() && !activity.isDestroyed()) {
                    try {
                        loadingDialog.dismiss();
                    } catch (Exception e) {

                    }
                }
            }

            loadingDialog = null;
            loadingDialog = new LoadingDialog(context);
        }
    }

    public void showLoading() {
        if (!ThreadUtils.isMainThread()) {
            return;
        }
        if (mContext == null) {
            return;
        }
        if (loadingDialog == null) {
            //todo loadingDialog is null
//            createLoading(mContext);
            return;
        }
        handler.removeMessages(1);
        if (mContext instanceof Activity) {
            Activity activity = (Activity) LoadingUtils.mContext;
            if (activity != null && !activity.isDestroyed() || !activity.isFinishing()) {
                loadingDialog.show();
                Message message = new Message();
                message.what = 1;
                handler.sendMessageDelayed(message, sendTime);
            }

        }
    }

    public void showLoading(String msg) {
        if (!ThreadUtils.isMainThread()) {
            return;
        }
        if (mContext == null) {
            return;
        }
        if (loadingDialog == null) {
            //todo loadingDialog is null
//            createLoading(mContext);
            return;
        }
        handler.removeMessages(1);
        if (mContext instanceof Activity) {
            Activity activity = (Activity) LoadingUtils.mContext;
            if (activity != null && !activity.isDestroyed() || !activity.isFinishing()) {
                loadingDialog.show(msg);
                Message message = new Message();
                message.what = 1;
                handler.sendMessageDelayed(message, sendTime);
            }

        }
    }

    public void dismissLoading() {
        if (!ThreadUtils.isMainThread()) {
            return;
        }
        if (loadingDialog == null) {
            //todo loadingDialog is null
        } else {
            try {
                loadingDialog.dismiss();
            } catch (Throwable e) {

            }
        }
    }

//    public void dismissLoading(long delayMillis) {
//        if (!ThreadUtils.isMainThread()) {
//            return;
//        }
//        handler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                dismissLoading();
//            }
//        }, delayMillis);
//
//    }


}
